home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SAMPLES.BIN / EMail.java < prev    next >
Encoding:
Java Source  |  1996-12-04  |  6.3 KB  |  185 lines

  1. /*
  2.     A basic extension of the java.applet.Applet class
  3.  */
  4.  
  5. import java.awt.*;
  6. import java.applet.*;
  7. import java.net.*;
  8. import java.io.*;
  9.  
  10. public class EMail extends Applet {
  11.     public void init() {
  12.         super.init();
  13.  
  14.         //{{INIT_CONTROLS
  15.         setLayout(null);
  16.         addNotify();
  17.         resize(650,530);
  18.         setBackground(new Color(12632256));
  19.         Title = new java.awt.Label("Simple E-Mail Applet",Label.CENTER);
  20.         Title.reshape(169,12,312,38);
  21.         Title.setFont(new Font("Dialog", Font.BOLD, 24));
  22.         add(Title);
  23.         keyPressManagerPanel1 = new symantec.itools.awt.KeyPressManagerPanel();
  24.         keyPressManagerPanel1.setLayout(null);
  25.         keyPressManagerPanel1.reshape(10,51,605,501);
  26.         add(keyPressManagerPanel1);
  27.         senderName = new java.awt.TextField();
  28.         senderName.reshape(138,-1,456,24);
  29.         keyPressManagerPanel1.add(senderName);
  30.         recipientName = new java.awt.TextField();
  31.         recipientName.reshape(138,36,456,24);
  32.         keyPressManagerPanel1.add(recipientName);
  33.         subjectLine = new java.awt.TextField();
  34.         subjectLine.reshape(138,72,456,24);
  35.         keyPressManagerPanel1.add(subjectLine);
  36.         messageBody = new java.awt.TextArea();
  37.         messageBody.reshape(138,108,456,228);
  38.         keyPressManagerPanel1.add(messageBody);
  39.         serverResponse = new java.awt.TextArea();
  40.         serverResponse.reshape(138,348,456,84);
  41.         keyPressManagerPanel1.add(serverResponse);
  42.         Send = new java.awt.Button("Send");
  43.         Send.reshape(138,441,108,24);
  44.         keyPressManagerPanel1.add(Send);
  45.         newButton = new java.awt.Button("New");
  46.         newButton.reshape(486,441,108,24);
  47.         keyPressManagerPanel1.add(newButton);
  48.         senderLabel = new java.awt.Label("From:");
  49.         senderLabel.reshape(14,2,96,23);
  50.         keyPressManagerPanel1.add(senderLabel);
  51.         messageLabel = new java.awt.Label("Message:");
  52.         messageLabel.reshape(14,110,96,23);
  53.         keyPressManagerPanel1.add(messageLabel);
  54.         subjectLabel = new java.awt.Label("Subject:");
  55.         subjectLabel.reshape(14,74,96,23);
  56.         keyPressManagerPanel1.add(subjectLabel);
  57.         serverRepsonseLabel = new java.awt.Label("Server Response:");
  58.         serverRepsonseLabel.reshape(14,351,132,24);
  59.         keyPressManagerPanel1.add(serverRepsonseLabel);
  60.         recipientLabel = new java.awt.Label("To:");
  61.         recipientLabel.reshape(14,38,96,23);
  62.         keyPressManagerPanel1.add(recipientLabel);
  63.         //}}
  64.     }
  65.  
  66.     public boolean handleEvent(Event event) {
  67.         if (event.target == Send && event.id == Event.ACTION_EVENT)
  68.         {
  69.             sendMessage();
  70.         }
  71.         if (event.target == newButton && event.id == Event.ACTION_EVENT)
  72.         {
  73.             prepForNewMessage();
  74.         }
  75.         return super.handleEvent(event);
  76.     }
  77.  
  78.     //Resets all TextFields and textArea in preparation for a new message.
  79.     void prepForNewMessage()
  80.     {
  81.         senderName.setText("");
  82.         recipientName.setText("");
  83.         subjectLine.setText("");
  84.         messageBody.setText("");
  85.         serverResponse.setText("");
  86.     }
  87.  
  88.     //This is the method responsible for establishing a communications
  89.     //path to the mail server and for sending SMTP commands.
  90.     //  Step 1:  Create a new socket object using the name of the mail
  91.     //             server and the port number.
  92.     //  Step 2:  Create a printstream object based on the new socket
  93.     //  Step 3:  Send the mail server the appropriate SMTP commands
  94.     //             using the printstream.
  95.     //  Step 4:  This sample also captures and displays status messages
  96.     //             return from the mail server.  See getReply() method
  97.     //             below.
  98.     //  Step 5:  Release the connection to the server.
  99.     //  Step 6:  Close the printstream and the socket.
  100.     void sendMessage()
  101.     {
  102.         //Insert sending host name below...
  103.         String sHostName = new String("mailservername.company.com");
  104.  
  105.         //Insert sendmail port number on sending host machin below...
  106.         //(Note:  Port will typically be port #25.)
  107.         int portNum = 25;
  108.  
  109.         try
  110.         {
  111.             //Step 1
  112.             Socket outgoing = new Socket(sHostName, portNum, true);
  113.  
  114.             //Step 2
  115.             PrintStream ps = new PrintStream(outgoing.getOutputStream());
  116.  
  117.             //Steps 3 & 4
  118.             ps.println("HELO " + sHostName);
  119.             serverResponse.appendText(getReply(outgoing) + "\n");
  120.  
  121.             ps.println("MAIL FROM: " + senderName.getText());
  122.             serverResponse.appendText(getReply(outgoing) + "\n");
  123.  
  124.             ps.println("RCPT TO: " + recipientName.getText());
  125.             serverResponse.appendText(getReply(outgoing) + "\n");
  126.  
  127.             ps.println("DATA");
  128.             serverResponse.appendText(getReply(outgoing) + "\n");
  129.  
  130.             ps.println("Subject: "  + subjectLine.getText() + "\n" + messageBody.getText() + "\n" + "." + "\n");
  131.             serverResponse.appendText(getReply(outgoing) + "\n");
  132.  
  133.             //Step 5 
  134.             ps.println("QUIT");
  135.             serverResponse.appendText(getReply(outgoing) + "\n");
  136.  
  137.             //Step 6            
  138.             ps.close();
  139.             outgoing.close();
  140.         }
  141.         catch (IOException Ex)
  142.         {
  143.             System.out.println(Ex.getMessage());
  144.         }
  145.     }
  146.  
  147.     //This method will capture status message returned by the mail server
  148.     //and display in the the Server Response TextArea on the Applet.
  149.     //Step 1:  Create an InputStream for the socket object created above that gets passed
  150.     //          into this method.
  151.     //Step 2:  Read a line of the InputStream and return the line as a string for display.
  152.     String getReply(Socket incoming)
  153.     {
  154.         try
  155.         {
  156.             //Step 1
  157.             DataInputStream myDIS = new java.io.DataInputStream(incoming.getInputStream());
  158.  
  159.             //Step 2            
  160.             return myDIS.readLine();
  161.         }
  162.         catch (java.io.IOException Ex)
  163.         {
  164.             return Ex.getMessage();
  165.         }
  166.     }
  167.  
  168.     //{{DECLARE_CONTROLS
  169.     java.awt.Label Title;
  170.     symantec.itools.awt.KeyPressManagerPanel keyPressManagerPanel1;
  171.     java.awt.TextField senderName;
  172.     java.awt.TextField recipientName;
  173.     java.awt.TextField subjectLine;
  174.     java.awt.TextArea messageBody;
  175.     java.awt.TextArea serverResponse;
  176.     java.awt.Button Send;
  177.     java.awt.Button newButton;
  178.     java.awt.Label senderLabel;
  179.     java.awt.Label messageLabel;
  180.     java.awt.Label subjectLabel;
  181.     java.awt.Label serverRepsonseLabel;
  182.     java.awt.Label recipientLabel;
  183.     //}}
  184. }
  185.